unit variacion;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Provider, SqlExpr, DBCtrls, DB, DBClient, DBLocal, DBLocalS,
  Grids, DBGrids, RXDBCtrl, StdCtrls, Mask, ToolEdit, ExtCtrls, DateUtils,
  Menus;

type
  TfrmVariacion = class(TForm)
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    grdMaterial: TRxDBGrid;
    cds: TSQLClientDataSet;
    s: TDataSource;
    cdsMATERIAL: TStringField;
    cdsMAXIMO: TFloatField;
    cdsMINIMO: TFloatField;
    cdsPORCENT_VARIACION: TFloatField;
    cdsProveedor: TSQLClientDataSet;
    cdsID_PROVEEDOR: TSmallintField;
    cdsProveedorID_PROVEEDOR: TSmallintField;
    cdsProveedorRAZON: TStringField;
    sProveedor: TDataSource;
    Panel1: TPanel;
    RxDBGrid1: TRxDBGrid;
    PopupMenu1: TPopupMenu;
    Verdetalle1: TMenuItem;
    cdsID_MATERIAL: TFloatField;
    edtFiltroProveedor: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    edtFiltroMaterial: TEdit;
    DBText1: TDBText;
    cdsCANT: TFloatField;
    cdspromVar: TAggregateField;
    Label3: TLabel;
    procedure cdsBeforeOpen(DataSet: TDataSet);
    procedure cdsProveedorAfterScroll(DataSet: TDataSet);
    procedure grdMaterialTitleClick(Column: TColumn);
    procedure Verdetalle1Click(Sender: TObject);
    procedure edtFiltroProveedorChange(Sender: TObject);
    procedure edtFiltroMaterialChange(Sender: TObject);
    procedure edtFechaIniChange(Sender: TObject);
    procedure grdMaterialDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private

  public
    constructor CreateNuevo(AOwner: TComponent);
  end;

var
  frmVariacion: TfrmVariacion;

implementation

uses MDatos, detalleMaterialCompra;

{$R *.dfm}


constructor TfrmVariacion.CreateNuevo(AOwner: TComponent);
begin
  inherited Create(Aowner);
  self.edtFechaIni.Date := IncYear(now, -1);
  self.edtFechaFin.Date := now;
  self.cdsProveedor.Open;
  self.ShowModal;
end;

procedure TfrmVariacion.cdsBeforeOpen(DataSet: TDataSet);
begin
  self.cds.Params[0].AsInteger := self.cdsProveedorID_PROVEEDOR.AsInteger;
  self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
  self.cds.Params[2].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
end;

procedure TfrmVariacion.cdsProveedorAfterScroll(DataSet: TDataSet);
begin
  self.cds.Close;
  self.cds.Open;
end;

procedure TfrmVariacion.grdMaterialTitleClick(Column: TColumn);
begin
  self.cds.IndexFieldNames := Column.FieldName;
end;

procedure TfrmVariacion.Verdetalle1Click(Sender: TObject);
begin
  TfrmDetalleMaterialCompra.CreateNuevo(self, FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText)),
  FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText))),
  self.cdsMATERIAL.AsString, self.cdsProveedorRAZON.AsString, self.cdsID_MATERIAL.asInteger);
end;

procedure TfrmVariacion.edtFiltroProveedorChange(Sender: TObject);
begin
  self.cdsProveedor.Filtered := False;
  self.edtFiltroMaterial.Clear;
  self.cdsProveedor.Filter := 'RAZON like ''%'+self.edtFiltroProveedor.Text+'%''';
  self.cdsProveedor.Filtered := True;
end;

procedure TfrmVariacion.edtFiltroMaterialChange(Sender: TObject);
begin
  self.cds.Filtered := False;
  self.cds.Filter := 'MATERIAL like ''%'+self.edtFiltroMaterial.Text+'%''';
  self.cds.Filtered := True;
end;

procedure TfrmVariacion.edtFechaIniChange(Sender: TObject);
begin
  try
    self.cdsProveedorAfterScroll(nil);
  except
  end;
end;

procedure TfrmVariacion.grdMaterialDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  valor: string;
  i: integer;
begin
  if (Column.FieldName = 'PORCENT_VARIACION') then
    if not Column.Field.IsNull then
    begin
      valor := FloatToStrF(StrToFloat(Column.Field.AsString), ffNumber,15, 2);
      if (Column.Field.Value < 0) then
        (Sender as TDBGrid).Canvas.Font.Color := clRed
      else
        (Sender as TDBGrid).Canvas.Font.Color := clBlack;
      i := Rect.Right-(Sender as TDBGrid).Canvas.TextWidth(valor+' ');
      (Sender as TDBGrid).Canvas.TextRect(Rect, i, Rect.Top + 2, valor);
    end;
end;

end.
